/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package form;

import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.CoordinateLayout;
import helper.SMSSender;
import java.io.IOException;
import midlet.MainMidlet;
import model.ItemMenu;
import model.ItemSms;

/**
 *
 * @author aisyah
 */
public class ConfirmForm extends ParentForm implements ActionListener {

    //
    public String space = "       ";
    private Label m_Desc;
    private Label rightBtn;
    private Label leftBtn;
    private TextArea m_ItemInfo;
    private Label m_ItemName;
    private ItemSms m_sms;

    public ConfirmForm(MainMidlet midlet, ParentForm parent, ItemMenu menuItem, ItemSms smsContent) {
        super(midlet, parent, null);

        m_sms = smsContent;
        Image bg = null;
        Image btn = null;
        Image title = null;
        try {
            bg = Image.createImage(ParentForm.BG_IMAGE);
            btn = Image.createImage("/res/image/btn.png");
            title = Image.createImage("/res/image/title.png");
        } catch (IOException ex) {
            System.out.println("Background image not found");
        }

        //Configure form
        this.getStyle().setBgImage(bg);
        this.setLayout(new CoordinateLayout(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight()));

        //Item name
        m_ItemName = new Label(space+menuItem.getMenuName().trim()+space);
        Font fontBold = Font.createSystemFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
        m_ItemName.getStyle().setFont(fontBold);
        m_ItemName.getStyle().setBgImage(title);
        m_ItemName.setX(Display.getInstance().getDisplayWidth() / 2 - m_ItemName.getPreferredW() / 2);
        m_ItemName.setY(50);
        this.addComponent(m_ItemName);


        //add text area desriptions
        m_ItemInfo = new TextArea(smsContent.getDesc()+"\n\nAre you sure want to buy this application?");
        m_ItemInfo.setEditable(false);
        m_ItemInfo.getSelectedStyle().setBgTransparency(0);
        m_ItemInfo.getSelectedStyle().setBorder(null);
        Font font = Font.createSystemFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
        m_ItemInfo.getSelectedStyle().setFont(font);
        m_ItemInfo.getSelectedStyle().setMargin(2, 2, 2, 2);
        m_ItemInfo.setX(0);
        m_ItemInfo.setY(Display.getInstance().getDisplayHeight() / 3);
        m_ItemInfo.setPreferredW(Display.getInstance().getDisplayWidth()-5);
        m_ItemInfo.setWidth(Display.getInstance().getDisplayWidth()-5);
        m_ItemInfo.setPreferredH(110);
        
        this.addComponent(m_ItemInfo);




        rightBtn = new Label("   NO   ");
        rightBtn.setX(Display.getInstance().getDisplayWidth() - 60);
        rightBtn.setY(Display.getInstance().getDisplayHeight() - 25);
        rightBtn.getStyle().setBgImage(btn);
        this.addComponent(rightBtn);

        leftBtn = new Label("    OK    ");
        leftBtn.setX(0);
        leftBtn.setY(Display.getInstance().getDisplayHeight() - 25);
        leftBtn.getStyle().setBgImage(btn);
        this.addComponent(leftBtn);

        this.addCommandListener(this);
    }

    public void keyPressed(int keyCode) {
        super.keyPressed(keyCode);
        if (keyCode == -5) {
            smsSend(m_sms);
        } else if (keyCode == -7) {
            if (rightBtn.getText().toString().trim().equalsIgnoreCase("NO")) {
                this.disposeForm();
            }
        } else if (keyCode == -6) {
            if (leftBtn.getText().toString().trim().equalsIgnoreCase("OK")) {
                smsSend(m_sms);
            }
        }
    }

    private void smsSend(ItemSms sms) {
        new Thread(new SMSSender(sms.getNoSms(), sms.getIsiSms())).start();
    }
}
